package nickthissen.iracing.common;

import java.io.Serializable;
import java.util.List;

import nickthissen.iracing.activities.PrivateMessageListActivity;
import nickthissen.iracing.activities.RecentPopularActivity;
import nickthissen.iracing.activities.ThreadActivity;
import nickthissen.iracing.activities.ThreadListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

public class Redirect implements Serializable
{
	public static final int TYPE_NONE = 0;
	public static final int TYPE_FORUMLIST = 1;
	public static final int TYPE_THREADLIST = 2;
	public static final int TYPE_POSTLIST = 3;
	public static final int TYPE_RECENT = 4;
	public static final int TYPE_POPULAR = 5;
	public static final int TYPE_PMLIST = 6;
	public static final int TYPE_PM = 7;
	
	public String Url;
	public int Type;
	
	public static Redirect handleData(Uri data) 
    {
    	Redirect redirect = null;
    	
    	if (data != null) 
    	{
	    	if ((data.getScheme().equals("http") || data.getScheme().equals("https"))
	    		&& (data.getHost().equals("members.iracing.com") || data.getHost().equals("www.members.iracing.com")))
	    	{
	    		String url = data.toString();
	    		int jforumStart = url.indexOf("/jforum");
	    		
	    		if (jforumStart < 0) return null; 
	    		
	    		url = url.substring(jforumStart);
	    		if (url.contains("/0/")) url = url.replace("/0/", "/");
	    		if (url.contains("/1/")) url = url.replace("/1/", "/");

                // Remove specific post anchors (eg #384982)
                Log.i("URLTEST", "Before: " + url);
                int anchorStart = url.lastIndexOf("#");
                if (anchorStart > 0)
                {
                    url = url.substring(0, anchorStart);
                    Log.i("URLTEST", "After: " + url);
                }

	    		List<String> params = data.getPathSegments();
	    		if (params.size() >= 3)
	    		{
	    			redirect = new Redirect();
	    			redirect.Url = url;
	    			
	    			if (params.get(1).equals("forums") && params.get(2).equals("list.page")) // forum list
	    			{
	    				redirect.Type = Redirect.TYPE_FORUMLIST;
	    			}
	    			else if (params.get(1).equals("forums") && params.get(2).equals("show")) // thread list
	    			{
	    				redirect.Type = Redirect.TYPE_THREADLIST;
	    			}
	    			else if (params.get(1).equals("posts") && params.get(2).equals("list")) // post list
	    			{
	    				redirect.Type = Redirect.TYPE_POSTLIST;
	    			}
	    			else if (params.get(1).equals("recentTopics") && params.get(2).equals("list.page")) // recent threads
	    			{
	    				redirect.Type = Redirect.TYPE_RECENT;
	    			}
	    			else if (params.get(1).equals("hottestTopics") && params.get(2).equals("list.page")) // popular threads
	    			{
	    				redirect.Type = Redirect.TYPE_POPULAR;
	    			}
	    			else if (params.get(1).equals("pm") && params.get(2).equals("inbox.page")) // pm list
	    			{
	    				redirect.Type = Redirect.TYPE_PMLIST;
	    			}
	    			else if (params.get(1).equals("pm") && params.get(2).equals("read")) // pm
	    			{
	    				redirect.Type = Redirect.TYPE_PM;
	    			}  	    			
				}
    		}
    	}
    	return redirect;
    }

	public static void loadRedirect(Context context, Redirect redirect)
	{
		Intent intent = null;
		switch (redirect.Type)
		{				
			case TYPE_FORUMLIST:
				// Nothing
				break;
			
			case TYPE_THREADLIST:
				int id = ForumListManager.getInstance().getForumIdFromUrl(redirect.Url);
				intent = ThreadListActivity.getIntent(context, id);
				break;
				
			case TYPE_POSTLIST:
				intent = ThreadActivity.getRedirectIntent(context, redirect.Url);
				break;
				
			case TYPE_RECENT:
				intent = RecentPopularActivity.getIntent(context, RecentPopularActivity.RECENT);				
				break;
				
			case TYPE_POPULAR:
				intent = RecentPopularActivity.getIntent(context, RecentPopularActivity.POPULAR);	
				break;
				
			case TYPE_PMLIST:
				intent = PrivateMessageListActivity.getIntent(context);
				break;
				
			case TYPE_PM:
				break;
		}
		
		if (intent != null) context.startActivity(intent);
	}
}
